#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>

int main(int argc, char const *argv[])
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);

    // 去除TIME_WAIT
    int on = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

    int bufsize;
    int len = sizeof(bufsize);
    getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &bufsize, &len);
    printf("初始的接收缓冲区大小：%d\n", bufsize);

    bufsize = 1024;
    setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(int));

    bufsize = 0;
    getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &bufsize, &len);
    printf("设置后接收缓冲区大小：%d\n", bufsize);



    // 获得水位线的大小
    int rcvlowat;
    len = sizeof(rcvlowat);
    getsockopt(sockfd, SOL_SOCKET, SO_RCVLOWAT, &rcvlowat, &len);
    printf("初始水位线的大小：%d\n", rcvlowat);

    rcvlowat = 100;
    setsockopt(sockfd, SOL_SOCKET, SO_RCVLOWAT, &rcvlowat, len);

    rcvlowat = 0;
    getsockopt(sockfd, SOL_SOCKET, SO_RCVLOWAT, &rcvlowat, &len);
    printf("设置后水位线的大小：%d\n", rcvlowat);


    struct sockaddr_in addr;
    len = sizeof(addr);
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(50006);
    if(bind(sockfd, (struct sockaddr *)&addr, len) == -1)
    {
        perror("bind() failed");
        exit(0);
    }
    listen(sockfd, 3);
    int connfd = accept(sockfd, NULL, NULL);

    char buf[100];
    read(connfd, buf, 100);
    printf("%s", buf);

    close(connfd);
    close(sockfd);

    return 0;
}
